package com.krtech.wecard.module.mayi.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.module.mayi.MayiPayEnums;
import com.krtech.wecard.module.mayi.entity.MayiCdkManage;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.mapper.MayiCdkManageMapper;
import com.krtech.wecard.module.mayi.mapper.MayiPayListMapper;
import com.krtech.wecard.module.mayi.service.MayiCdkManageService;
import com.krtech.wecard.module.mayi.service.MayiPayListService;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
@Slf4j
public class MayiCdkBugFIxTask {

    @Autowired
    private MayiPayListMapper payListMapper;

    @Autowired
    private MayiPayListService mayiPayListService;

    @Autowired
    private MayiCdkManageMapper cdkManageMapper;

    @Autowired
    private MayiCdkManageService manageService;

    @Autowired
    private SysKeyValueService keyValueService;

//    @Scheduled(cron = "0 0/10 * * * ?")
    @Scheduled(cron = "0/30 * * * * ? ")
    public void liuliangTask(){
        //获得系数

        //定时修复所有还有余量的卡
        List<MayiCdkManage> bugFixCdkList = cdkManageMapper.getBugFixCdk();
        for (MayiCdkManage bugFixCdk:bugFixCdkList){
            //现在存在一个问题，算的是
//            Long cdkUse = payListMapper.getCdkUse(bugFixCdk.getCdkCode());
            //查询这张cdk所有的数据
            List<MayiPayList> payLists = mayiPayListService.list(new QueryWrapper<MayiPayList>()
                    .eq(MayiPayList.COL_CDK_CODE, bugFixCdk.getCdkCode()));

            //计算这张cdk最后的剩余量,这个tm的是使用的量！！！！！
            Integer currentUse = 0;

            if (payLists.size() > 0){
                for (MayiPayList payList:payLists){
                    if (payList.getType() != null && (payList.getType().equals(MayiPayEnums.TYPE_DIANZAN) || payList.getType().equals(MayiPayEnums.TYPE_REVIEW))){

                        //分析当前这条数据得情况，
                        //1. 判断reachCount是否大于等于realGoalCount ，如果是，则这条任务已经执行完毕
                        if (payList.getReachCount() >= payList.getGoalCount()){
                            currentUse = currentUse + payList.getGoalCount();
                        }

                        //判断，reachCount小于realGoalCount，
                        if (payList.getReachCount() < payList.getGoalCount()){
                            //只有在等待的没有结束时间

                            //计算有结束时间的
                            if (payList.getEndTime() != null){
                                //由于系数经常改变，所以会导致计算不准确，所以考虑如何不使用系数
                                //计算之后的剩余
                                int computeRemaing = new BigDecimal(payList.getReachCount()).divide(new BigDecimal(payList.getCoefficient()), 0, BigDecimal.ROUND_HALF_UP).intValue();
                                currentUse = currentUse + computeRemaing;
                            }

                            //没有结束时间的算全部(没有结束时间，说明是正在排队等待)
                            if (payList.getEndTime() == null){
                                currentUse = currentUse + payList.getGoalCount();
                            }
                        }
                    }else {

                        //分析当前这条数据得情况，
                        //1. 判断reachCount是否大于等于realGoalCount ，如果是，则这条任务已经执行完毕
                        if (payList.getReachCount() > payList.getRealGoalCount()){
                            currentUse = currentUse + payList.getGoalCount();
                        }

                        //判断，reachCount小于realGoalCount，
                        if (payList.getReachCount() < payList.getRealGoalCount()){
                            //只有在等待的没有结束时间

                            //计算有结束时间的
                            if (payList.getEndTime() != null){
                                //由于系数经常改变，所以会导致计算不准确，所以考虑如何不使用系数
                                //计算之后的剩余
                                int computeRemaing = new BigDecimal(payList.getReachCount()).divide(new BigDecimal(payList.getCoefficient()), 0, BigDecimal.ROUND_HALF_UP).intValue();
                                currentUse = currentUse + computeRemaing;
                            }

                            //没有结束时间的算全部(没有结束时间，说明是正在排队等待)
                            if (payList.getEndTime() == null){
                                currentUse = currentUse + payList.getGoalCount();
                            }
                        }
                    }
                }
            }



            Long currentUseLong = currentUse.longValue();

            if (!bugFixCdk.getCurrentUse().equals(currentUseLong)){
                log.info("cdk号码:"+bugFixCdk.getCdkCode()+"不一致，当前使用为:"+bugFixCdk.getCurrentUse()+",实际使用为:"+currentUseLong);

                bugFixCdk.setCurrentUse(currentUseLong);


                manageService.updateById(bugFixCdk);
            }

        }
    }
}
